<template>
    <div class="main">
        <!-- 景区列表 -->
        <el-card class="box-card">
            <el-form :inline="true" :model="page" class="demo-form-inline">
                <el-form-item label="名称">
                    <el-input v-model="page.name" placeholder="请输入" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">搜索</el-button>
                    <el-button type="primary" @click="reset">重置</el-button>
                </el-form-item>
            </el-form>
            <el-button type="primary" style="margin-bottom: 30px;" @click="addEdiClick(1)">添加</el-button>
            <el-table :data="tableData" style="width: 100%" max-height="600px">
                <el-table-column type="index" width="50" />
                <el-table-column prop="name" label="名称" align="center" />
                <el-table-column label="图片" align="center" width="200">
                    <template #default="scope">
                        <img style="width: 100px;height: 50px;" :src="scope.row.photo" alt="">
                    </template>
                </el-table-column>
                <el-table-column prop="sort" label="排序" align="center" />
                <el-table-column prop="recommendationRate" label="热力指数" align="center" />
                <el-table-column prop="createTime" label="更新时间" align="center" />
                <el-table-column fixed="right" label="操作" width="160">
                    <template #default="scope">
                        <el-button link type="primary" size="small" @click="addEdiClick(2, scope.row)">编辑</el-button>
                        <el-button link type="danger" size="small" @click="delData(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <Pagination v-if="total != 0" :page="page" :total="total" @pageSizeChanged="handlePageSizeChange"
                @currentPageChanged="handleCurrentPageChange" />
        </el-card>
    </div>
    <!-- 新增修改 -->
    <el-dialog v-model="dialogVisible" :title="isAdd ? '新增' : '修改'" width="500px" top="15vh"
        :before-close="handleClose">
        <Add v-if="dialogVisible" @closeOK="closeOK" @closeAdd="handleClose" :isAdd="isAdd" :row="rowData.row" />
    </el-dialog>
</template>

<script setup>
import Add from './add.vue' // 引入组件
import { commodityRecommendationList,commodityRecommendationDelete} from '@/api/goods.js'
import { onMounted, reactive, ref } from 'vue'
import Pagination from '@/components/pagination/index.vue' // 引入分页组件
import { ElMessage, ElMessageBox } from 'element-plus'

const dialogVisible = ref(false)
const page = reactive({
    name: '',
    pageNum: 1,
    pageSize: 10
})
const total = ref(0)
const tableData = reactive([])
const isAdd = ref(false)
const rowData = reactive({
    row: {}
})
// 搜索
const onSubmit = () => {
    getList()
}
// 重置
const reset = () => {
    page.name = "";
    page.pageNum = 1;
    page.pageSize = 10;
    getList()
}
// 分页
const handlePageSizeChange = (val) => {
    page.pageNum = 1
    page.pageSize = val
    getList()
}

const handleCurrentPageChange = (val) => {
    page.pageNum = val
    // page.pageSize = 10
    getList()
}
// 确认弹框
const closeOK = () => {
    dialogVisible.value = false;
    getList()
}
// 删除
const delData = (scope) => {
    ElMessageBox.confirm(
        '是否删除该条数据?',
        '删除',
        {
            confirmButtonText: 'OK',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            commodityRecommendationDelete(scope.id).then(res => {

                if (res.code == 200) {
                    ElMessage({
                        type: 'success',
                        message: '删除成功',
                    })
                    getList()
                }
            })

        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消',
            })
        })
}
// 弹框关闭
const handleClose = () => {
    dialogVisible.value = false
}
// 新增
const addEdiClick = (num, row) => {
    dialogVisible.value = true
    if (num == 2) {
        isAdd.value = false
        rowData.row = row
    } else {
        isAdd.value = true

    }
}

onMounted(() => {
    getList()
})
// 获取列表
const getList = () => {
    tableData.length = 0
    total.value = 0
    commodityRecommendationList(page).then(res => {
        if (res.code == 200) {
            tableData.push(...res.data.records)
            total.value = res.data.total
        }
    })
}
</script>

<style lang="scss" scoped>
.box-card :deep(.el-select__wrapper) {
    width: 100px;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}

.main {
    padding: 0 5px 5px 5px;
}
</style>